<?php
class  Masuk extends CI_Controller
{
    public function __construct()   {
            parent::__construct(); 
			$this->load->model("masuk_model"); 
    }
	
    public function index()
    {
		$this->load->view('masuk');
	}
	
	function doLogin(){
		$username      = $this->input->post('username');
        $sandi   = $this->input->post('sandi');
	

		$this->form_validation->set_rules('username','Username','required');
		$this->form_validation->set_rules('sandi','Sandi','required');
		$this->form_validation->set_message('required','%s harus diisi');

		if($this->form_validation->run()==false){
			$this->index();
		}else{
        	$sandi   = md5($sandi);
        	$cek=$this->masuk_model->doLogin($username,$sandi);
        	if($cek!=false){
				$data = array (
					'isLogin' => 'yes',
					'id'=>$cek['id_pengguna'],
					'id_peg'=>$cek['id_peg'],
					'nama'=>$cek['nama'],
					'user_id' => $cek['username'],
					'level' => $cek['role']
				);
				$this->session->set_userdata($data);
				redirect('admin');				
        	}else{
				$this->session->set_flashdata('error_login', 'Maaf, Username atau sandi anda salah !');
            	redirect('masuk');
        	}
		}
	}	

	function logOut(){
		$this->session->unset_userdata('isLogin');
		$this->session->unset_userdata('user_id');
		$this->session->unset_userdata('level');
		$this->session->sess_destroy();
		$this->session->set_flashdata('error_login', 'Anda Sudah Log Out!');
		redirect('masuk');
	}
}
?>